package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.ChanPinXiangQingDaoimpl;
import com.ysd.entity.ChanPinXiangQing;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminChanPingXiangQingServlet extends BaseServlet {
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=getAllChanPinXiangQing
	 * 获得所有产品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAllChanPinXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();
		ArrayList<ChanPinXiangQing> cpxqList=cpxqDao.getAllChanPinXiangQing();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(cpxqList));
		
		out.flush();
		out.close();
		
	}
	
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=delChanPinXiangQingId
	 * 根据id产出产品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delChanPinXiangQingId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();
		
		int chanPingId=Integer.parseInt(request.getParameter("chanPingId"));
		int n=cpxqDao.delChanPingXiangQingId(chanPingId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		if(n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(true, "删除失败", "您的操作有误"));
		}


		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=getChanPingXiangQingWhere
	 * 	产品分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChanPingXiangQingWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String imgs=request.getParameter("imgs");
		String chanPinName=request.getParameter("chanPinName");
		String chanPinDiZhi=request.getParameter("chanPinDiZhi");
		String chanPinPaiZi=request.getParameter("chanPinPaiZi");
		String shiFouGuoqi=request.getParameter("shiFouGuoqi");
		String shiFouXianLiang=request.getParameter("shiFouXianLiang");
		String baoZhiQi=request.getParameter("baoZhiQi");
		String jiaGongFang=request.getParameter("jiaGongFang");
		String kouWei=request.getParameter("kouWei");
		String shenChangShang=request.getParameter("shenChangShang");
		String zhongLiang=request.getParameter("zhongLiang");
		String leiMing=request.getParameter("leiMing");
		
		
		int chanPingId=0;
		int chanPinShouJia=0;
		int chanPinShiChangJia=0;
		int fenleiId=0;
		
		int qiShiJiaGe=0;
		String qsjg=request.getParameter("qiShiJiaGe");
		if(qsjg!=null && !qsjg.equals("")) {
			qiShiJiaGe=Integer.parseInt(qsjg);
		}
		
		int jieShuJiaGe=0;
		String jsjg=request.getParameter("jieShuJiaGe");
		if(jsjg!=null && !jsjg.equals("")) {
			jieShuJiaGe=Integer.parseInt(jsjg);
		}
		
		
		String cpid=request.getParameter("chanPingId");
		if(cpid!=null && !cpid.equals("")) {
			chanPingId=Integer.parseInt(cpid);
		}
		
		String cpsj=request.getParameter("chanPinShouJia");
		if(cpsj!=null && !cpsj.equals("")) {
			chanPinShouJia=Integer.parseInt(cpsj);
		}
		
		String cpscj=request.getParameter("chanPinShiChangJia");
		if(cpscj!=null && !cpscj.equals("")) {
			chanPinShiChangJia=Integer.parseInt(cpscj);
		}
		
		String flid=request.getParameter("fenleiId");
		if(flid!=null && !flid.equals("")) {
			fenleiId=Integer.parseInt(flid);
		}
		
		
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		ChanPinXiangQing cpxq=new ChanPinXiangQing(chanPingId, chanPinName, chanPinDiZhi, chanPinShouJia, chanPinShiChangJia, chanPinPaiZi, shiFouGuoqi, shiFouXianLiang, baoZhiQi, jiaGongFang, kouWei, shenChangShang, zhongLiang, fenleiId, imgs, leiMing, qiShiJiaGe, jieShuJiaGe);
		ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();
		
		int size=Integer.parseInt(request.getParameter("size"));
		int page=Integer.parseInt(request.getParameter("page"));
		ArrayList<ChanPinXiangQing> chanPingXiangQingList=cpxqDao.getChanPingXiangQingWhere(cpxq,page,size);
		
		Gson gson=new Gson();
		out.print(gson.toJson(chanPingXiangQingList));
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=getChanPingXiangQingWhereByPage
	 * 查询多条件的长度
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChanPingXiangQingWhereByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String imgs=request.getParameter("imgs");
		String chanPinName=request.getParameter("chanPinName");
		String chanPinDiZhi=request.getParameter("chanPinDiZhi");
		String chanPinPaiZi=request.getParameter("chanPinPaiZi");
		String shiFouGuoqi=request.getParameter("shiFouGuoqi");
		String shiFouXianLiang=request.getParameter("shiFouXianLiang");
		String baoZhiQi=request.getParameter("baoZhiQi");
		String jiaGongFang=request.getParameter("jiaGongFang");
		String kouWei=request.getParameter("kouWei");
		String shenChangShang=request.getParameter("shenChangShang");
		String zhongLiang=request.getParameter("zhongLiang");
		String leiMing=request.getParameter("leiMing");
		
		
		int chanPingId=0;
		int chanPinShouJia=0;
		int chanPinShiChangJia=0;
		int fenleiId=0;
		
		int qiShiJiaGe=0;
		String qsjg=request.getParameter("qiShiJiaGe");
		if(qsjg!=null && !qsjg.equals("")) {
			qiShiJiaGe=Integer.parseInt(qsjg);
		}
		
		int jieShuJiaGe=0;
		String jsjg=request.getParameter("jieShuJiaGe");
		if(jsjg!=null && !jsjg.equals("")) {
			jieShuJiaGe=Integer.parseInt(jsjg);
		}
		
		
		String cpid=request.getParameter("chanPingId");
		if(cpid!=null && !cpid.equals("")) {
			chanPingId=Integer.parseInt(cpid);
		}
		
		String cpsj=request.getParameter("chanPinShouJia");
		if(cpsj!=null && !cpsj.equals("")) {
			chanPinShouJia=Integer.parseInt(cpsj);
		}
		
		String cpscj=request.getParameter("chanPinShiChangJia");
		if(cpscj!=null && !cpscj.equals("")) {
			chanPinShiChangJia=Integer.parseInt(cpscj);
		}
		
		String flid=request.getParameter("fenleiId");
		if(flid!=null && !flid.equals("")) {
			fenleiId=Integer.parseInt(flid);
		}
		
		
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		ChanPinXiangQing cpxq=new ChanPinXiangQing(chanPingId, chanPinName, chanPinDiZhi, chanPinShouJia, chanPinShiChangJia, chanPinPaiZi, shiFouGuoqi, shiFouXianLiang, baoZhiQi, jiaGongFang, kouWei, shenChangShang, zhongLiang, fenleiId, imgs, leiMing, qiShiJiaGe, jieShuJiaGe);
		ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();

		ArrayList<ChanPinXiangQing> chanPingXiangQingList=cpxqDao.getChanPingXiangQingWhereByPage(cpxq);
		
		Gson gson=new Gson();
		out.print(gson.toJson(chanPingXiangQingList.size()));
		
		out.flush();
		out.close();
	}
	
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=addChanPingXiangQing
	 * 添加产品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addChanPingXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();
		String imgs=request.getParameter("imgs");
		String chanPinName=request.getParameter("chanPinName");
		String chanPinDiZhi=request.getParameter("chanPinDiZhi");
		String chanPinPaiZi=request.getParameter("chanPinPaiZi");
		String shiFouGuoqi=request.getParameter("shiFouGuoqi");
		String shiFouXianLiang=request.getParameter("shiFouXianLiang");
		String baoZhiQi=request.getParameter("baoZhiQi");
		String jiaGongFang=request.getParameter("jiaGongFang");
		String kouWei=request.getParameter("kouWei");
		String shenChangShang=request.getParameter("shenChangShang");
		String zhongLiang=request.getParameter("zhongLiang");
		
		int chanPinShouJia=0;
		String cpsj=request.getParameter("chanPinShouJia");
		if(cpsj!=null&&!cpsj.equals("")) {
			chanPinShouJia=Integer.parseInt(cpsj);
		}
		int chanPinShiChangJia=0;
		String cpscj=request.getParameter("chanPinShiChangJia");
		if(cpscj!=null&&!cpscj.equals("")) {
			chanPinShouJia=Integer.parseInt(cpscj);
		}
		int fenleiId=Integer.parseInt(request.getParameter("fenleiId"));
		ChanPinXiangQing cpxq=new ChanPinXiangQing(0, chanPinName, chanPinDiZhi, chanPinShouJia, chanPinShiChangJia, chanPinPaiZi, shiFouGuoqi, shiFouXianLiang, baoZhiQi, jiaGongFang, kouWei, shenChangShang, zhongLiang, fenleiId, imgs);
		ArrayList<ChanPinXiangQing> cpxqList=cpxqDao.selChanPingXiangQingByName(chanPinName);
		if(cpxqList.size()==0) {
			int n=cpxqDao.addChanPingXiangQing(cpxq);
			if(n>0) {
				out.print(Result.toClient(true, "添加成功", ""));
			}else {
				out.print(Result.toClient(false, "添加失败", ""));
			}
		}else {
			out.print(Result.toClient(false, "该商品已存在", ""));
		}
		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=getChanPinXiangQingbyId
	 * 通过id查询商品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChanPinXiangQingbyId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();
		int chanPingXiangQingId=Integer.parseInt(request.getParameter("chanPingXiangQingId"));
		ChanPinXiangQing cpxq=cpxqDao.getChanPinXiangQingbyId(chanPingXiangQingId);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(cpxq));
		
		out.flush();
		out.close();
		
	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=editChanPinXiangQing
	 * 修改产品详情
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void editChanPinXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();
		
		String imgs=request.getParameter("imgs");
		String chanPinName=request.getParameter("chanPinName");
		String chanPinDiZhi=request.getParameter("chanPinDiZhi");
		String chanPinPaiZi=request.getParameter("chanPinPaiZi");
		String shiFouGuoqi=request.getParameter("shiFouGuoqi");
		String shiFouXianLiang=request.getParameter("shiFouXianLiang");
		String baoZhiQi=request.getParameter("baoZhiQi");
		String jiaGongFang=request.getParameter("jiaGongFang");
		String kouWei=request.getParameter("kouWei");
		String shenChangShang=request.getParameter("shenChangShang");
		String zhongLiang=request.getParameter("zhongLiang");
		
		int chanPingId=0;
		String cpid=request.getParameter("chanPingId");
		if(cpid!=null&&!cpid.equals("")) {
			chanPingId=Integer.parseInt(cpid);
		}
		
		
		int chanPinShouJia=0;
		String cpsj=request.getParameter("chanPinShouJia");
		if(cpsj!=null&&!cpsj.equals("")) {
			chanPinShouJia=Integer.parseInt(cpsj);
		}
		int chanPinShiChangJia=0;
		String cpscj=request.getParameter("chanPinShiChangJia");
		if(cpscj!=null&&!cpscj.equals("")) {
			chanPinShouJia=Integer.parseInt(cpscj);
		}
		int fenleiId=Integer.parseInt(request.getParameter("fenleiId"));
		ChanPinXiangQing cpxq=new ChanPinXiangQing(chanPingId, chanPinName, chanPinDiZhi, chanPinShouJia, chanPinShiChangJia, chanPinPaiZi, shiFouGuoqi, shiFouXianLiang, baoZhiQi, jiaGongFang, kouWei, shenChangShang, zhongLiang, fenleiId, imgs);
		ArrayList<ChanPinXiangQing> cpxqList=cpxqDao.selChanPingXiangQingByName(chanPinName);
		
		int n=cpxqDao.editChanPinXiangQing(cpxq);
		
		if(cpxqList.size()==0) {
			
			if(n>0) {
				out.print(Result.toClient(true, "修改成功", ""));
			}else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		}else {
			out.print(Result.toClient(false, "该名称已存在", ""));
		}
		out.flush();
		out.close();
	}
	
	
	
/**
 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=upload	
 * 上传图片
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		SmartUpload smart=new  SmartUpload();
		smart.setCharset("UTF-8");
		smart.initialize(getServletConfig(), request, response);
		
		try {
			smart.upload();
			
			
			String name=System.currentTimeMillis()+".jpg";
			smart.getFiles().getFile(0).saveAs("/images/"+name);
			out.print(Result.toClient(true, "上传成功", name));
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
		
		out.flush();
		out.close();
}



/**
 * localhost:8080/xiyangyangchaoshi/adminchanpingxiangqing?reqName=selChanPingXiangQingId
  * 根据产品id查询长度
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void selChanPingXiangQingId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	ChanPinXiangQingDaoimpl cpxqDao=new ChanPinXiangQingDaoimpl();
	ArrayList<ChanPinXiangQing> cpxqlength=cpxqDao.selChanPingXiangQingId();
	response.setCharacterEncoding("UTF-8");
	PrintWriter out=response.getWriter();
	
	Gson gson=new Gson();
	out.print(gson.toJson(cpxqlength.size()));
	
	out.flush();
	out.close();
	
}




}
